<?php

class CompanyApp extends MemberbaseApp
{
	var $_company_mod;
    function __construct()
    {
        $this->CompanyApp();
    }
    function CompanyApp()
    {
        parent::__construct();
		$this->_company_mod =& m('company');
    }
	
    function index()
    {

		$page = $this->_get_page();
		$companys = $this->_company_mod->find(array(
			'order' => 'company_id ASC',
			'count' => true,
			'limit' => $page['limit']
		));
		$this->getSubBrand($companys);
		$page['item_count'] = $this->_company_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('companys', $companys);
        $this->display('company.index.html');
        /* 当前位置 */
        $this->_curlocal(LANG::get('company_manage'));
    }
	
	function add()
	{
		
		if (!IS_POST) {
			$mod_member=&m("member");
			$member_list=$mod_member->find(array('conditions'=>'role='.USER_MERCHANDISER_ADMIN));
			$this->assign("member_list",$member_list);
			$this->_assign_form();
	        $this->display('company.form.html');
        	/* 当前位置 */
        	$this->_curlocal(LANG::get('company_manage'), 'index.php?app=company',
				Lang::get('add_new_company'));
		} else {
			$company_name = trim($_POST['company_name']);
			$description = trim($_POST['description']);
			$if_show = intval($_POST['if_show']);
			$mod_company_manager=&m("company_manager");
			if (empty($company_name)) {
				$this->show_warning('company_name_not_empty');
				return;
			}
			if (!$this->_company_mod->unique($company_name)) {
				$this->show_warning('company_name_is_exist');
				return;
			}
			
			$data = array(
				'company_name' => $company_name,
				'eng_company_name'=>trim($_POST['eng_company_name']),
				'company_address'=>trim($_POST['company_address']),
				'company_site'	=> trim($_POST['company_site']),
				'tel'	=> trim($_POST['tel']),
				'fax'	=> trim($_POST['fax']),
				'add_time' => time()
			);
			
			$company_id = $this->_company_mod->add($data);
			if (!$company_id) {
				$this->_error($this->_company_mod->get_error());
				return;
			}
			if(!empty($_POST["uid"]))
			{
			  if(!$mod_company_manager->saveData($company_id,$_POST["uid"]))
			  {
				  $this->show_warning('保存主管失败!');
				  return;
			  }
			}
			$this->show_message('add_company_success', Lang::get('company_manage'), 'index.php?app=company');
		}
	}
	
	function _assign_form()
	{
		$company = array(
			'if_show' => 1,
		);
		$this->assign('company', $company);
	}
	
	function edit()
	{
		$company_id = intval($_GET['company_id']);
		if (!$company_id) {
			$this->show_warning('company_not_exit');
		}
		$mod_company_manager=&m("company_manager");

		if (!IS_POST) {
			$mod_member=&m("member");
			$member_list=$mod_member->find(array('conditions'=>'role='.USER_MERCHANDISER_ADMIN));
			$manager_list=$mod_company_manager->getManagerList($company_id);
			foreach($manager_list as $key=>$item)
			{
				if(isset($member_list[$key]))
				{
					$member_list[$key]['is_select']=1;
				}
			}
			
			$this->assign("member_list",$member_list);
			$company = $this->_company_mod->get_info($company_id);
			$this->assign('company', $company);
			
	        $this->display('company.form.html');
        	/* 当前位置 */
        	$this->_curlocal(LANG::get('company_manage'), 'index.php?app=company',
				Lang::get('edit_company'));
		} else {
			$company_name = trim($_POST['company_name']);
			$if_show = intval($_POST['if_show']);
			
			if (empty($company_name)) {
				$this->show_warning('company_name_not_empty');
				return;
			}
			if (!$this->_company_mod->unique($company_name, $company_id)) {
				$this->show_warning('company_name_is_exist');
				return;
			}
			
			$data = array(
				'company_name' => $company_name,
				'eng_company_name'=>trim($_POST['eng_company_name']),
				'company_address'=>trim($_POST['company_address']),
				'company_site'	=> trim($_POST['company_site']),
				'tel'	=> trim($_POST['tel']),
				'fax'	=> trim($_POST['fax'])
			);
			$this->_company_mod->edit($company_id, $data);
			if ($this->_company_mod->get_error()) {
				$this->_error($this->_company_mod->get_error());
				return;
			}
			if(!empty($_POST["uid"]))
			{
			  if(!$mod_company_manager->saveData($company_id,$_POST["uid"]))
			  {
				  $this->show_warning('保存主管失败!');
				  return;
			  }
			}
			$this->show_message('edit_company_success', Lang::get('company_manage'), 'index.php?app=company');
		}
	}

	function del()
	{
		$company_id = intval($_GET['company_id']);
		if (!$company_id) {
			$this->show_warning('company_not_exit');
			return;
		}

		$company_mod=&m("company");
		$company_mod->drop("company_id={$company_id}");
		if ($company_mod->get_error()) {
			$this->_error($company_mod->get_error());
			return;
		}
		
		$this->show_message('删除公司成功', Lang::get('company_manage'), 'index.php?app=company');
		
	}
		
	function check_company()
	{
		$company_name = empty($_GET['company_name']) ? null : trim($_GET['company_name']);
		$company_id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$company_name) {
            echo ecm_json_encode(false);
            return;
		}
		echo ecm_json_encode($this->_company_mod->unique($company_name, $company_id));
	}
	
	function getSubBrand(&$company_list)
	{
		
		$company_ids=array_keys($company_list);
		$brand_mod =& m('brand');
		$brands=$brand_mod->find(array(
		'conditions' =>"if_show =1 AND company_id".db_create_in($company_ids),
		'order'		=>'brand_id'
		));
		$temp_arr=array();
		foreach($brands as $item)
		{
			$temp_arr[$item['company_id']][]=$item["brand_name"];
		}
		foreach($temp_arr as $key=>$item)
		{
			if(isset($company_list[$key]))
			{
			$company_list[$key]['sub_brand']=implode(",",$item);
			}
		}		
	}
}

?>
